<?php
namespace app\common\model;

use app\admin\model\WarehouseGeneral;
use app\admin\model\WarehouseGeneralSku;
use think\Db;
use think\Model;
use think\facade\Log as SystemLog;

class ProductOut extends Model
{
    /**
     * 处理清仓订单 清仓后减库存
     * @param $out_id
     * @param $number
     * @return bool
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function trueOutOrder($out_id,$number){
        //获取清仓商品信息
        $model = $this->where(["id"=>$out_id])->find();
        if($model == null){
            SystemLog::record("[清仓支付回调]当前总订单进入订单回调", 'alert');
            return false;
        }

        //减批次库存
        Db::startTrans();
        $warehouseGeneralSkuService = new WarehouseGeneralSku();
        $generalSkuModel = $warehouseGeneralSkuService->where(["id"=>$model["general_sku_id"]])->find();
        if($generalSkuModel["stock"] - $number < 0){
            Db::rollback();
            return false;
        }
        $res = $warehouseGeneralSkuService->where(["id"=>$model["general_sku_id"]])->setDec("stock",$number);
        if(!$res){
            Db::rollback();
            return false;
        }

        //减批次对应仓库库存
        //select * from om_warehouse_general where product_id = 18 and warehouse_id = 1
        $warehouseGeneralService = new WarehouseGeneral();
        $generalModel = $warehouseGeneralService->where(["product_id"=>$generalSkuModel["product_id"],"warehouse_id"=>$generalSkuModel["warehouse_id"]])->find();
        if($generalModel["stock"] - $number < 0){
            Db::rollback();
            return false;
        }
        $res = $warehouseGeneralService->where(["product_id"=>$generalSkuModel["product_id"],"warehouse_id"=>$generalSkuModel["warehouse_id"]])->setDec("stock",$number);
        if(!$res){
            Db::rollback();
            return false;
        }

        Db::commit();
        return true;
    }

    /**
     * 清仓订单取消
     * @param $out_id
     * @param $number
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function falseOutOrder($out_id,$number){
        //获取清仓商品信息
        $model = $this->where(["id"=>$out_id])->find();
        if($model == null){ return false;}

        //增加批次库存
        Db::startTrans();
        $warehouseGeneralSkuService = new WarehouseGeneralSku();
        $generalSkuModel = $warehouseGeneralSkuService->where(["id"=>$model["general_sku_id"]])->find();
        $res = $warehouseGeneralSkuService->where(["id"=>$model["general_sku_id"]])->setInc("stock",$number);
        if(!$res){
            Db::rollback();
            return false;
        }

        //增加批次对应仓库库存
        //select * from om_warehouse_general where product_id = 18 and warehouse_id = 1
        $warehouseGeneralService = new WarehouseGeneral();
        $generalModel = $warehouseGeneralService->where(["product_id"=>$generalSkuModel["product_id"],"warehouse_id"=>$generalSkuModel["warehouse_id"]])->find();
        if($generalModel["stock"] - $number < 0){
            Db::rollback();
            return false;
        }
        $res = $warehouseGeneralService->where(["product_id"=>$generalSkuModel["product_id"],"warehouse_id"=>$generalSkuModel["warehouse_id"]])->setDec("stock",$number);
        if(!$res){
            Db::rollback();
            return false;
        }
        Db::commit();
        return true;
    }
}